import Ember from "ember";


var oneDay = 1000 * 60 * 60 * 24,
    oneMin = 1000 * 60;

function dateFromISO8601(isoDateString) {
  var parts = isoDateString.match(/\d+/g);
  var isoTime = Date.UTC(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5]);
  var isoDate = new Date(isoTime);

  return isoDate;
}

export default Ember.Handlebars.makeBoundHelper(function(value, options) {
  if (value) {
    var t = (typeof value);
    if (t === 'number') {
      value = new Date(value);
    } else if (t === 'string') {
      value = dateFromISO8601(value);
    }

    var start = new Date().getTime(),
        elapsed = start - value.getTime();

    if (elapsed < oneMin) {
      return "刚刚";
    } else if (elapsed < oneDay) {
      return value.getHours() + ":" + value.getMinutes() + ":" + value.getSeconds();
    } else {
      return value.getFullYear() + "." + (value.getMonth() + 1) + "." + value.getDate();
    }
  } else {
    return "";
  }
});
